package com.mod.reanimator 
{
	import com.mod.data.dtLevel;
	import com.mod.Global;
	import flash.display.MovieClip;

	public class HeartSkin {
		
		public var mLife:int = 300;
		private var mClip:MovieClip;
		
		public var onFinishDie:Function;
		
		private var mIsDie:Boolean = false;
		
		private var healthBar:MovieClip;
		
		private var mCounterEnterBase:int = 0;
		private var mDataLevel:dtLevel;
		
		public function get counterEnterBase():int { return mCounterEnterBase; }
		
		public function addCounterEnterBase(value:int):void {
			mCounterEnterBase += value;
			// draw
			setText(mCounterEnterBase + "/" + mDataLevel.numEnterBaseWin);
			
			if (mCounterEnterBase >= mDataLevel.numEnterBaseWin) {
				// on Win Game
				Global.engine.onStartWinLevel();
			}

		}
		
		private function setText(value:String):void {
			if (mClip.parent) {
				var p:MovieClip = MovieClip(mClip.parent);
				if (p.txt_enter_base) {
					p.txt_enter_base.text = value;
				}
			}			
		}
		
		public function get clip():MovieClip { return mClip;  }
		
		public static const POSX_HEARTH:Number = 244.45;
		public static const POSY_HEARTH:Number = 189;
		private static const POSX_HEARTH2:Number = 252.5;
		public static const POSY_HEARTH2:Number = 185;
		public static const POSX_HEARTH3:Number = 250.45;
		public static const POSY_HEARTH3:Number = 172.4;
		public function HeartSkin(clip:MovieClip) {
			mClip = clip;
			mClip.endHit = endHit;
			mClip.endDie = endDie;
			mClip.gotoAndStop("normal");
			healthBar = Global.project.getMovie("barBase");
			mClip.parent.addChild(healthBar); 
			switch(Global.currentLevel)
			{
				case 0: healthBar.x = POSX_HEARTH; healthBar.y = POSY_HEARTH ; break;
				case 1: healthBar.x = POSX_HEARTH2; healthBar.y = POSY_HEARTH2; break;
				case 2: healthBar.x = POSX_HEARTH3; healthBar.y = POSY_HEARTH3; break;
			}
			
			mDataLevel = Global.dataGame.levels[Global.currentLevel];
			setText("0/" + mDataLevel.numEnterBaseWin);
		}
		
		
		private function endDie():void {
			if (onFinishDie != null) {
				onFinishDie();
			}
			
		}
		
		public function endHit():void {
			mClip.gotoAndStop("normal");
		}
		
		public function update(dt:int):void {
			
		}
		
		public function onReceiveDamage(value:int):void {
			if (mIsDie) { return;  }
			mLife += value;
			healthBar.filler_mc.x = (mLife * 120) / 300;
			Global.playSound("snd_heartbeat", 1, 20);
			trace( "mLife : " + mLife );
			if (mLife <= 0) {
				mLife = 0;
				mIsDie = true;
				onAnimationDie();
			} else {
				onAnimationHit();
			}
		}
		
		private function onAnimationHit():void {
			mClip.gotoAndStop("damaged");
		}
		
		public function onAnimationDie():void {
			mClip.gotoAndStop("death");
		}
		
		public function clean():void {
			mClip = null;
		}
		
	}

}